package com.nz.net.hays.www;

import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import com.nz.net.hays.www.SignIn.loginAccess;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnClickListener;

public class ProfileView extends Activity implements OnClickListener{
	
	
	private String moutputusername, moutfirstname;
	private TextView mtextviewusernme;
	private EditText mtxtFirstname;
	private Button buttonUpdate;
	private ProgressDialog pDialog;
	private EditText firstname, lastname, phoneno, gender, resume;
	int flag=0;
	Intent i;
	JSONParser jsonParser = new JSONParser();
	private static String url = "http://hays.byethost6.com/haysupdate.php";
	private static final String TAG_SUCCESS = "success"; 
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
		.detectDiskReads().detectDiskWrites().detectNetwork()
		.penaltyLog().build());
		super.onCreate(savedInstanceState);
		 setContentView(R.layout.profile_view);
		Intent profileViewIntent=getIntent();
		moutputusername=profileViewIntent.getExtras().getString("username");
		//moutfirstname=profileViewIntent.getExtras().getString("firstn");
		
		setContentView(R.layout.profile_view);
		mtextviewusernme = (TextView) findViewById(R.id.textviewusername);
		mtextviewusernme.setText("Hi "+moutputusername);
		//mtxtFirstname=(EditText)findViewById(R.id.firstname);
		//mtxtFirstname.setText(moutfirstname);
		//logout = (Button)findViewById(R.id.buttonlogout);
		//logout.setOnClickListener(this);
		
		setupWidget();
		
		buttonUpdate=(Button)findViewById(R.id.buttonupdate);
		firstname=(EditText)findViewById(R.id.firstname);
		lastname=(EditText)findViewById(R.id.lastname);
		phoneno=(EditText)findViewById(R.id.phone);
		gender=(EditText)findViewById(R.id.gender);
		resume=(EditText)findViewById(R.id.resume);
		
		buttonUpdate.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				
				new loginAccess().execute();
			}
			private boolean isOnline(Context mContext) {
				ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
				NetworkInfo netInfo = cm.getActiveNetworkInfo();
				if (netInfo != null && netInfo.isConnectedOrConnecting())
			   	  {
					return true;
		     		}
				    return false;
		       	}
		});
		
    }
	
	class loginAccess extends AsyncTask<String, String, String> {

		private	String user;
		private	String pwd;
		private String firstname2, lastname2, phoneno2, gender2, resume2;
			protected void onPreExecute() {
				super.onPreExecute();
				pDialog = new ProgressDialog(ProfileView.this);
				pDialog.setMessage("Updating...");
				pDialog.setIndeterminate(false);
				pDialog.setCancelable(true);
				pDialog.show();
			}
			@Override
			protected String doInBackground(String... arg0) {
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				//user=username.getText().toString();
				//pwd=password.getText().toString();
				user=moutputusername;
				firstname2=firstname.getText().toString();
				lastname2=lastname.getText().toString();
				phoneno2=phoneno.getText().toString();
				gender2=gender.getText().toString();
				resume2=resume.getText().toString();
				params.add(new BasicNameValuePair("username", user));
				params.add(new BasicNameValuePair("firstname", firstname2));
				params.add(new BasicNameValuePair("lastname", lastname2));
				params.add(new BasicNameValuePair("phoneno", phoneno2));
				params.add(new BasicNameValuePair("gender", gender2));
				params.add(new BasicNameValuePair("resume", resume2));
				JSONObject json = jsonParser.makeHttpRequest(url,"POST", params);
				Log.d("Create Response", json.toString());
				
				try {
					int success = json.getInt(TAG_SUCCESS);
					//int success=1;
					if (success == 1) 
					 {
						publishProgress("");
					  flag=0;	
					   
					//  i.putExtra("password",pwd);
					 
					 
					 
					 }
					 else
					 {
						// failed to login
						 publishProgress("Incorrect username/password");
						flag=1;
					}
				} catch (Exception e) {
					e.printStackTrace();
					Toast.makeText(ProfileView.this,e.getMessage(), Toast.LENGTH_LONG).show();
					
				}
				return null;
			}
			
			 
			protected void onPostExecute(String file_url) {
				try {
					pDialog.dismiss();
					
					if(flag==0)
					{
//						 i = new Intent(getApplicationContext(),WelcomeWithDrawerActivity.class);
//						  
//						 
//						 i.putExtra("username",user);
//						startActivity(i);
//						 finish();
					}
					else if(flag==1)
						{
						//Toast.makeText(SignIn.this,"Please Enter Correct informations", Toast.LENGTH_LONG).show();
						}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			
			
		  }
	
	private void setupWidget(){
		buttonUpdate=(Button)findViewById(R.id.buttonupdate);
		buttonUpdate.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v){
		try{
			if (v.getId()==R.id.buttonupdate)
			{
				Intent btnUpdate = new Intent(getApplicationContext(),Update.class);
				startActivity(btnUpdate);
				
			}
			 if(v.getId()==R.id.imagebuttonregister)
			{
				Intent registeractivity = new Intent(getApplicationContext(),SignUp.class);
				startActivity(registeractivity);
				
			}
			 if(v.getId()==R.id.imagebuttonlogin)
			{
				Intent loginactivity = new Intent(getApplicationContext(),SignIn.class);
				startActivity(loginactivity);	
			}
			}
			catch(Exception e)
			{
				String msg=e.getMessage();
				Toast.makeText(getApplicationContext(),msg, Toast.LENGTH_LONG).show();
				
			}
	}

	
}